package pt.ipl.isel.deetc.leic.pdm.g12.yamba;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DetailActivity extends BaseActivity implements OnClickListener {
	private Button share;
	private String shareEmailBody;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail);
		
		Bundle bundle = getIntent().getExtras();			
		
		((TextView)findViewById(R.id.detailId)).setText("" + (Long)bundle.get("id"));
		((TextView)findViewById(R.id.detailUser)).setText((String)bundle.get("user"));
		((TextView)findViewById(R.id.detailTime)).setText((String)bundle.get("time"));
		((TextView)findViewById(R.id.detailStatus)).setText((String)bundle.get("status"));
		
		shareEmailBody = (String)bundle.get("time") + " " + (String)bundle.get("user") + ": " + (String)bundle.get("status");
		
		share = (Button) findViewById(R.id.buttonShare);
		share.setOnClickListener(this);
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.removeItem(R.id.refreshTimeline);
		
		return super.onPrepareOptionsMenu(menu);
	}
	
	public void onClick(View v) {				
		Log.d("DetailActivity", "onClick");
		
		AlertDialog.Builder alert = new AlertDialog.Builder(this);  

        alert.setTitle(getString(R.string.shareTitle));  
        alert.setMessage(getString(R.string.shareDesc));  

        // Set an EditText view to get user input   
        final EditText inputName = new EditText(this);  
        alert.setView(inputName); 
        
        alert.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int whichButton) {  
                String email = inputName.getText().toString();
                
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("text/plain");
                i.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
                i.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.shareEmailSubject));
                i.putExtra(Intent.EXTRA_TEXT, shareEmailBody);
                try {
                    startActivity(Intent.createChooser(i, "Mailing..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(DetailActivity.this, getString(R.string.shareEmailNoClients), Toast.LENGTH_SHORT).show();
                }
            }  
        });
        
        alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int whichButton) {  
                
            }  
        });
        
        alert.show();
	}
}
